$.extend({

	gallerys : {} ,

	gallery : function(options){
		
	
		if($.gallerys[options.swapId]){
			return $.gallerys[options.swapId];
		}
		

		var gallery = {} ;
		gallery.options = options;
		

		gallery.scope = $("#" + gallery.options.swapId).find("ul[data-gallery]");
		

		gallery.domScope = gallery.scope[0];
		

		gallery.desc = $(gallery.scope).siblings("p[data-role='desc']");
		
	
		gallery.anchor = $(gallery.scope).siblings("div[data-role='count-list']");
		
	
		var p_obj = $(gallery.scope).parent();
		gallery.width = $(p_obj).css({
			"overflow" : "hidden"
		}).width() || $('body').width();

		var scope_obj = $(gallery.scope).parents(".e-sect");
		var ml = parseInt($(scope_obj).css("margin-left")||0);
		var mr = parseInt($(scope_obj).css("margin-right")||0);
		gallery.width -= ml + mr ;

	
		$(gallery.scope).css({
			"width": 10000
		});
		
		$(scope_obj).css("width",gallery.width);
			
		
		gallery.reload = function(){
			var len = $("li", gallery.scope).css({
				"width": gallery.width
			}).length;
			var html = '' ;
			for(var i = 0 ;i < len ;i ++){
				html += '<a href="javascript:;" data-index="'+i+'"></a>' ;
			}
			$(gallery.anchor).html(html);
			
		
			$("a", gallery.anchor).unbind("click").bind("click",function(){
				gallery.click(this);
			});
			

			$("a:first", gallery.anchor).addClass("active");
			
			var html = $("li:nth-child(1) .desc",gallery.scope).html();
			if (html) {
				$(gallery.desc).show();

				$("span", gallery.desc).html(html);
			}else{
				$(gallery.desc).hide();
			}
			
			$(gallery.scope).attr("data-index",0).attr("data-count",len-1);
			
			gallery.index = 0;
			gallery.count = len - 1 ;

			gallery.height = $(gallery.scope).parent().height();
			
		
			return gallery;
		}
		
		
		gallery.add = function(){
			$("li:last", gallery.scope).css({
				width : gallery.width
			});
			var len = $("li", gallery.scope).length - 1 ;
			$(gallery.anchor).append('<a href="javascript:;" data-index="'+len+'"></a>');
			

			$("a:last", gallery.anchor).unbind("click").bind("click",function(){
				gallery.click(this);
			});
	
			if(len == 0){
				$("a:first", gallery.anchor).addClass("active");
				
				var html = $("li:nth-child(1) .desc",gallery.scope).html();
				if (html) {
					$(gallery.desc).show();
	
					$("span", gallery.desc).html(html);
				}else{
					$(gallery.desc).hide();
				}
				
				$(gallery.scope).attr("data-index",len);
				gallery.index = 0;
			}
			$(gallery.scope).attr("data-count",len);
			
			gallery.count = len ;
		}

		gallery.click = function(dot){

			gallery.stop();
			
			$(dot).addClass("active").siblings().removeClass("active");
			

			var index = parseInt($(dot).attr("data-index"));
			gallery.index = index ;
			
			$(gallery.scope).attr("data-index",index).animate({
				"margin-left" : index * gallery.width * -1
			},500,function(){
				var html = $("li:nth-child("+(index+1)+") .desc",gallery.scope).html();
				if (html) {
					$(gallery.desc).show();
	
					$("span", gallery.desc).html(html);
				}else{
					$(gallery.desc).hide();
				}

				gallery.start();
			});
		};
		

		gallery.time = null ;

		gallery.index = 0 ;

		gallery.count = 0;

		gallery.isAuto = false;

		gallery.interval = 0 ;
		

		gallery.setIsAuto = function(flag){
			gallery.isAuto = flag ;
			return gallery ;
		}
		

		gallery.stop = function(){
			if (gallery.time) {
				clearInterval(gallery.time);
				gallery.time = null;
			}
		}
		

		gallery.start = function(interval){
			// 鍏堝仠姝�
			gallery.stop();
			if(!gallery.isAuto || (!interval && gallery.interval == 0))
				return ;
			

			interval = parseInt(interval || 2000);
			
			gallery.interval = interval;

			gallery.time = setInterval(gallery.auto,interval);
		}
		

		gallery.auto = function(){
			gallery.index = parseInt((gallery.index >= gallery.count && "0" ) || (gallery.index + 1) );
			
			$(gallery.scope).attr("data-index",gallery.index).animate({
				"margin-left" : gallery.index * gallery.width * -1
			},400,function(){
				var html = $("li:nth-child("+(gallery.index+1)+") .desc",gallery.scope).html();
				if (html) {
					$(gallery.desc).show();
		
					$("span", gallery.desc).html(html);
				}else{
					$(gallery.desc).hide();
				}

				$("a:nth-child("+(gallery.index+1)+")",gallery.anchor).addClass("active").siblings().removeClass("active");
			});
		}
		
		var isClick = false;
		var isScroll = null ;
		var x = 0 ;
		var y = 0 ;

		$(gallery.scope).bind("mousedown",function(event){
			isClick = true;
			x = event.offsetX ;
	
			gallery.stop();
		}).bind("mousemove",function(event){ 
			
			if(isClick){
				var nowX = event.offsetX ;
				$(this).animate({
					"marginLeft" : "+=" + (nowX > x ? 3 : -3)
				},0);
			}
		}).bind("mouseout",function(event){  
			gallery.maquree(event);
			isClick = false;
			x = 0;
		});
		
		
	
		$(document).bind("mouseup",function(event){  
			gallery.maquree(event);
			isClick = false;
			x = 0;
			y = 0 ;
		});
		

		$(gallery.scope).bind("touchstart",function(event){

			isClick = true;
			isScroll = null ; 
			x = event["originalEvent"]["changedTouches"][0]["pageX"] ;
			y = event["originalEvent"]["changedTouches"][0]["pageY"] ;

			gallery.stop();
		})
		
	
		$(gallery.scope).bind("touchmove",function(event){
			if(isScroll === true){
				isClick = false ;
				return ;
			}
			if(isClick){
				var nowX = event["originalEvent"]["changedTouches"][0]["pageX"] ;
				var nowY = event["originalEvent"]["changedTouches"][0]["pageY"] ;
				if(Math.abs(nowY - y) > 20 && isScroll === null){
					isScroll = true ;
					isClick = false ;
					return ;
				}
				if(Math.abs(nowX - x) > 20 && isScroll === null){
					isScroll = false ;
				}else if(isScroll === null){
					return ;
				}
				if (isScroll === false) {
					event.preventDefault();
					$(this).animate({
						"marginLeft": "+=" + (nowX > x ? 1 : -1)
					}, 0);
				}
			}
		});
		

		$(gallery.scope).bind("touchend",function(event){
			if (isClick) {
				event.preventDefault();
				gallery.maquree(event);
				isClick = false;
				x = 0;
			}
		});
		

		$(document).bind("mouseup",function(event){  
			gallery.maquree(event);
			isClick = false;
			x = 0;
		});
		

		gallery.maquree = function(event){
			if (isClick) {
				var index = parseInt($(gallery.scope).attr("data-index"));
				var count = parseInt($(gallery.scope).attr("data-count"));
				
				var nowX = event["originalEvent"]["changedTouches"] ? event["originalEvent"]["changedTouches"][0]["pageX"] : event.offsetX;
				if(nowX > x + 20 && index > 0){ 
					index -- ;
				}else if(nowX < x - 20 && count > index){ 
					index ++ ;
				}
				$(gallery.scope).attr("data-index",index).animate({
					"margin-left" : index * gallery.width * -1
				},200,function(){
					var html = $("li:nth-child("+(index+1)+") .desc",gallery.scope).html() ;
					if (html) {
						$(gallery.desc).show();
				
						$("span", gallery.desc).html(html);
					}else{
						$(gallery.desc).hide();
					}
				});
				gallery.index = index ;

				$("a:nth-child("+(index+1)+")",gallery.anchor).addClass("active").siblings().removeClass("active");
	
				gallery.start();
			}
		}
		

		$.gallerys[gallery.options.swapId] = gallery;
		return gallery ;
	}
});